<?php
namespace App\Model;

use function AlibabaCloud\Client\json;
use Illuminate\Database\Eloquent\Model;
use App\Model\ShopGoodModel;
use App\Model\ShopLableModel;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\SoftDeletes;

//后台模块表
class ShopOrderFsModel extends Model
{
	use SoftDeletes;
    //定义一个表名
    protected $table = 'shop_order_fs';
    //定义主键
    protected $primaryKey = 'id';

    static function shop($type,$status = 0){
    	$data = [];
		$start = date('Y-m-d 00:00:00',strtotime("-1 days"));
		$end = date('Y-m-d 23:59:59',strtotime("-1 days"));
    	switch($type){
			case '1':
				$list = ShopGoodModel::OrderBy('sale_num','desc')->where('state',1)->limit(3)->get();
				$data = ShopOrderFsModel::order_for($list);
				break;
			case '2':
				$list = DB::table('shop_order_fs')->select('shop_order_fs.gid')->distinct()->join('shop_good','shop_good.id','shop_order_fs.gid')->where('shop_good.state',1)->whereBetween('shop_order_fs.created_at',[$start,$end])->get();
				$data = ShopOrderFsModel::orderfs_for($list,1);
				break;
			case '3':
				$list = DB::table('store_good')->select('store_good.gid')->distinct()->join('shop_good','store_good.gid','shop_good.id')->where('shop_good.state',1)->get();
				$list = json_decode($list,true);
				$data = ShopOrderFsModel::good_for($list,1);

				break;
			case '4':
				$list = DB::table('store_good')->select('store_good.gid')->distinct()->where('shop_good.state',1)->join('shop_good','store_good.gid','shop_good.id')->whereBetween('store_good.created_at',[$start,$end])->get();
				$list = json_decode($list,true);
				$data = ShopOrderFsModel::good_for($list,1);
				break;

			case '5':
				$list = ShopGoodModel::OrderBy('sale_num','desc')->where('state',1)->limit(10)->get();

				$data = ShopOrderFsModel::order_for($list,2);
				break;
			case '6':
				$list = DB::table('shop_order_fs')->select('shop_order_fs.gid')->distinct()->join('shop_good','shop_good.id','shop_order_fs.gid')->where('shop_good.state',1)->whereBetween('shop_order_fs.created_at',[$start,$end])->get();
				$data = ShopOrderFsModel::orderfs_for($list,2);
				break;
			case '7':
				$list = DB::table('store_good')->join('shop_good','store_good.gid','shop_good.id')->select('store_good.gid')->distinct()->where('shop_good.state',1)->limit(10)->get();
				$list = json_decode($list,true);
				$data = ShopOrderFsModel::good_for($list,2);

				break;
			case '8':
				$list = DB::table('store_good')->join('shop_good','store_good.gid','shop_good.id')->select('store_good.gid')->distinct()->where('shop_good.state',1)->whereBetween('store_good.created_at',[$start,$end])->limit(10)->get();
				$list = json_decode($list,true);
				$data = ShopOrderFsModel::good_for($list,2);
				break;
			case '9':
				$list = ShopGoodModel::OrderBy('id','desc')->where('state',1)->paginate(10);
				$data = ShopOrderFsModel::order_for($list);
				break;
			case '10':
				$tm = ShopLabelModel::where('name','特卖')->value('id');
				$list = ShopGoodModel::Where('state',1)->where('labels','like',"%,".$tm.",%")->paginate(10);
				$data = ShopOrderFsModel::order_for($list);
				break;
			case '11':
				$list = ShopGoodModel::OrderBy('sale_num','desc')->where('state',1)->paginate(10);
				$data = ShopOrderFsModel::order_for($list);
				break;
			case '12':
				$list = ShopGoodModel::OrderBy('sell_price','asc')->where('state',1)->paginate(10);
				$data = ShopOrderFsModel::order_for($list);
				break;
			case '13':
				$list = ShopGoodModel::OrderBy('sell_price','desc')->where('state',1)->paginate(10);
				$data = ShopOrderFsModel::order_for($list);
				break;

		}
		return $data;
	}

	static function order_for($list){
    	$data = [];
    	if(count($list)>0){
			foreach($list as $k=>$v){
				//商品ID
				$data[$k]['id'] = $v['id'];
				//商品名
				$data[$k]['name'] = $v['name'];
				if($v['title']){
					$data[$k]['name'] = $v['title'];
				}
				//图片
				$data[$k]['picname'] = "http://".$_SERVER['HTTP_HOST'].$v['picpath'].$v['picname'];
				if($v['oss_pic']){
					$data[$k]['picname'] = $v['oss_pic'];
				}
				//零售价
				$data[$k]['single_price'] = number_format($v['sell_price'],2,".","");
				//已入库
				$data[$k]['store_num'] = StoreGoodModel::where('gid',$v['id'])->sum('num');
				//销量
				$data[$k]['sell_num'] = $v['sale_num'];

				$label_res = [];
				$label = explode(',',$v['labels']);
				foreach($label as $key=>$val){
					$label_info = ShopLabelModel::where('id',$val)->first();
					if($label_info){
						$label_res[] = $label_info['color'];
					}
				}
				$data[$k]['label'] = array_values($label_res);
				//专柜价
				$data[$k]['del_coin'] = $v['del_coin'];

			}
			return array_values($data);
		}else{
    		return $data;
		}

	}

	static function orderfs_for($ids,$type=0){
		$data = [];
		if(count($ids) > 0){
			foreach($ids as $k=>$v){
				//商品信息
				$good = ShopGoodModel::where('id',$v->gid)->first();
				if($good['state'] != 1){
					continue;
				}
				//商品ID
				$data[$k]['id'] = $good['id'];
				//商品名
				$data[$k]['name'] = $good['name'];
				if($good['title']){
					$data[$k]['name'] = $good['title'];
				}
				//图片
				$data[$k]['picname'] = "http://".$_SERVER['HTTP_HOST'].$good['picpath'].$good['picname'];
				if($good['oss_pic']){
					$data[$k]['picname'] = $good['oss_pic'];
				}
				//零售价
				$data[$k]['single_price'] = number_format($good->sell_price,2,".","");
				//已入库
				$data[$k]['store_num'] = StoreGoodModel::where('gid',$v->gid)->sum('num');
				//销量
				$data[$k]['sell_num'] = $good->sale_num;

				$label_res = [];
				$label = explode(',',$good['labels']);
				foreach($label as $key=>$val){
					$label_info = ShopLabelModel::where('id',$val)->first();
					if($label_info){
						$label_res[] = $label_info['color'];
					}
				}
				$data[$k]['label'] = array_values($label_res);
				//专柜价
				$data[$k]['del_coin'] = $good['del_coin'];
			}
			$last_names = array_column($data,'sell_num');
			array_multisort($last_names,SORT_DESC,$data);
			if($type == 1){
				array_slice($data,0,3);
			}

			return array_values($data);
		}else{
			return $data;
		}

	}

	static function good_for($list,$type=0){
    	$data = [];
    	if(count($list) > 0){
    		$arr = '';
			foreach($list as $k=>$v){
				$arr .= $v['gid'].",";
			}
			$ids = array_unique(explode(",",trim($arr,',')));
			foreach ($ids as $k=>$v){
				$good = ShopGoodModel::where('id',$v)->first();
				//商品ID
				$data[$k]['id'] = $good['id'];
				//商品名
				$data[$k]['name'] = $good['name'];
				if($good['title']){
					$data[$k]['name'] = $good['title'];
				}
				//图片
				$data[$k]['picname'] = "http://".$_SERVER['HTTP_HOST'].$good['picpath'].$good['picname'];
				if($good['oss_pic']){
					$data[$k]['picname'] = $good['oss_pic'];
				}
				//零售价
				$data[$k]['single_price'] = number_format($good['sell_price'],2,".","");
				//已入库
				$data[$k]['store_num'] = StoreGoodModel::where('gid',$good['id'])->sum('num');
				//销量
				$data[$k]['sell_num'] = $good['sale_num'];

				$label_res = [];
				$label = explode(',',$good['labels']);
				foreach($label as $key=>$val){
					$label_info = ShopLabelModel::where('id',$val)->first();
					if($label_info){
						$label_res[] = $label_info['color'];
					}
				}
				$data[$k]['label'] = array_values($label_res);
				//专柜价
				$data[$k]['del_coin'] = $good['del_coin'];

			}
			$last_names = array_column($data,'store_num');
			array_multisort($last_names,SORT_DESC,$data);
			if($type == 1){
				array_slice($data,0,3);
			}

			return array_values($data);
		}
    	return $data;

	}

	//查询子订单
	static function getSon($id){
		$data = [];
    	$son_list = ShopOrderFs::where('oid',$id)->where('is_delete',2)->get();
		if($son_list){
			foreach($son_list as $k=>$v){
				$data[$k]['id'] = $v->id;
				$data[$k]['sn_son'] = $v['sn_son'];
				//订单状态(1待发货  2待收货  3已完成)
				$data[$k]['state'] = $v['state'];
				switch($v['state']){
					case '1':
						$data[$k]['state_name'] = '待发货';
						break;
					case '2':
						$data[$k]['state_name'] = '待收货';
						break;
					case '3':
						$data[$k]['state_name'] = '已完成';
						break;
				}
				if($v['shop_attr']){
					$data[$k]['shop_attr'] = $v['shop_attr'];
				}else{
					$data[$k]['shop_attr'] = '无规格';
				}

				$data[$k]['picname'] = "http://".$_SERVER['HTTP_HOST'].$v['shop_pic'];
				$data[$k]['shop_name'] = $v['shop_name'];
				$good = ShopGoodModel::where('id',$v['gid'])->first();
				if($good['title']){
					$data[$k]['shop_name'] = $good['title'];
				}

				$data[$k]['shop_price'] = number_format($v['single_price'],2,".","");
				$data[$k]['shop_num'] = $v['num'];
			}
		}
		return $data;
	}



}
